from flask import Blueprint, request
import json

file_blue_print = Blueprint("file_blue_print", __name__, url_prefix="/file")

test_file = None


def load_test_file():
    with open("../flask_demo/fake_data/test_file.json", 'r', encoding="utf-8") as f:
        global test_file
        try:
            test_file = json.load(f)
        except Exception:
            print("err: load_test_file: ")
        pass
    pass


@file_blue_print.route("/release")
def get_file_release():
    global test_file
    if test_file is None:
        load_test_file()
    res = {
        "success": True,
        "data": test_file,
    }
    return res


@file_blue_print.route("/publish", methods=["POST",])
def post_file_publish():
    global test_file
    print(request.args, request.form)
    user_id = request.json.get("userId")
    content = request.json.get("content")
    print(user_id, content)
    if user_id is not None and content is not None:
        test_file = content
        pass
    return {
        "success": True,
    }